package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import com.example.test.dialogfactory.customerview.RecyclerPageBridge;
import com.example.test.dialogfactory.factory.DataFactory;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import easier.recycler.DataViewHolder;
import easier.recycler.TypeViewHolder;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.functions.Consumer;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

public class MainActivity5 extends AppCompatActivity {

    private SmartRefreshLayout mRefresh;
    private RecyclerView mRecycler;
    private DataFactory mFactory;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity5.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main5 );
        initView();
        initViewAction();
    }

    private void initView() {

        mRefresh = (SmartRefreshLayout) findViewById( R.id.refresh );
        mRecycler = (RecyclerView) findViewById( R.id.recycler );
    }

    private void initViewAction() {

        mRecycler.setLayoutManager( new LinearLayoutManager( this ) );
        Bridge bridge = new Bridge( mRecycler );

        mFactory = new DataFactory();

        mRefresh.setOnRefreshListener( new OnRefreshListener() {
            @Override
            public void onRefresh( @NonNull RefreshLayout refreshLayout ) {

                bridge.refresh();
            }
        } );
        mRefresh.setOnLoadMoreListener( new OnLoadMoreListener() {
            @Override
            public void onLoadMore( @NonNull RefreshLayout refreshLayout ) {

                bridge.loadMore();
            }
        } );

        mRecycler.post( new Runnable() {
            @Override
            public void run() {

                mRefresh.autoRefresh();
            }
        } );
    }

    private class Bridge extends RecyclerPageBridge< String > {

        public Bridge( RecyclerView recyclerView ) {

            super( recyclerView );
        }

        @Override
        protected Observable< List< String > > requestPageData( RecyclerView recyclerView, int page, int pageSize ) {

            return mFactory.createPageData( page, pageSize )
                .delay( 2, TimeUnit.SECONDS )
                .observeOn( AndroidSchedulers.mainThread() )
                .doOnNext( new Consumer< List< String > >() {
                    @Override
                    public void accept( List< String > strings ) throws Throwable {

                        if ( page == 1 && strings.size() < pageSize ) {
                            mRefresh.finishLoadMoreWithNoMoreData();
                        }
                    }
                } );
        }

        @Override
        protected TypeViewHolder buildViewHolder( RecyclerView recyclerView, String itemData, int position ) {

            return new Holder( itemData );
        }

        @Override
        protected void notifyFinish( RecyclerView recyclerView, List< Page< String > > page, int mode, boolean success ) {

            super.notifyFinish( recyclerView, page, mode, success );
            if ( mode == 1 ) {
                mRefresh.finishRefresh();
            }
            if ( mode == 2 ) {
                mRefresh.finishLoadMore();
            }
        }

        public class Holder extends DataViewHolder< String > {

            public Holder( String s ) {

                super( R.layout.recycler_item_00, s );
            }

            private final OnClickListener mOnClickListener = new OnClickListener() {
                @Override
                public void onClick( View v ) {

                    reCreatePage( new OnReCreatePage< String >() {
                        @Override
                        public boolean onReCreate( int page, List< String > pageData ) {

                            int index = 0;
                            for ( String s : pageData ) {
                                if ( Objects.equals( getElement(), s ) ) {
                                    pageData.remove( index );
                                    return true;
                                }
                                index++;
                            }

                            return false;
                        }
                    } );
                }
            };

            @Override
            protected void onBind( ViewHolder viewHolder, View root, int position, int type, List< Object > payloads ) {

                ((TextView) root.findViewById( R.id.text )).setText( getElement() );

                root.setOnClickListener( mOnClickListener );
            }

            @Override
            public boolean itemEquals( @Nullable TypeViewHolder obj ) {

                if ( obj instanceof Holder ) {
                    return Objects.equals( getElement(), ((Holder) obj).getElement() );
                } else {
                    return false;
                }
            }

            @Override
            public boolean contentEquals( @Nullable TypeViewHolder obj ) {

                if ( obj instanceof Holder ) {
                    return Objects.equals( getElement(), ((Holder) obj).getElement() );
                } else {
                    return false;
                }
            }
        }
    }
}